package at.fhooe.mc.android.shiftalarmclock;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MyArrayAdapter extends ArrayAdapter<DummyDataMain> implements
		OnClickListener {

	private static final String LOG_TAG = "MyAdapter";

	// internal list of the relevant data

	public MyArrayAdapter(Context _context, int _textViewResourceId,
			ArrayList<DummyDataMain> listMain) {

		super(_context, _textViewResourceId, listMain);
	}

	@SuppressLint("ShowToast")
	@Override
	public View getView(int _position, View _convertView, ViewGroup _parent) {

		Log.i(LOG_TAG, "in getView()");
		if (_convertView == null) {
			Context c = getContext();
			LayoutInflater inflater = (LayoutInflater) c
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			_convertView = inflater.inflate(R.layout.list_item_layout, null);
		}

		final DummyDataMain element = this.getItem(_position);
		if (element != null) {

			TextView v;
			v = (TextView) _convertView.findViewById(R.id.textView1);

			String s;
			if (element.getAlarmTime() == "") {
				s = (element.getShift());
			} else {
				s = (element.getShift() + ": " + element.getAlarmTime());
			}
			v.setText(s);

			final ToggleButton tb = (ToggleButton) _convertView
					.findViewById(R.id.toggleButton1);
			tb.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View _v) {
					switch (_v.getId()) {
					// set Alarm On/Off and give ah Toast message
					case R.id.toggleButton1: {
						if (AlarmModel.AlarmOnOff == true) {
							AlarmModel.AlarmOnOff = false;
							Toast toast = Toast.makeText(getContext(),
									R.string.text_alarm_on, 2000);
							toast.show();
							Log.i("AlarmClock", "ToggleButton On pressed");
						} else {
							AlarmModel.AlarmOnOff = true;
							Toast toast = Toast.makeText(getContext(),
									R.string.text_alarm_off, 2000);
							toast.show();
							Log.i("AlarmClock", "ToggleButton Off pressed");
						}
					}
						break;
					}
				}
			});
		}
		return _convertView;

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}
}